package com.atguigu.team.service;

import com.atguigu.team.domain.*;

import static com.atguigu.team.service.Data.*;
/**
 * @author zhouzhou
 * @version 1.0
 * @description:
 * 功能：负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法。
 * 说明：
 * employees用来保存公司所有员工对象
 * NameListService()构造器：
 * 根据项目提供的Data类构建相应大小的employees数组
 * 再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，以及相关联的Equipment子类的对象
 * 将对象存于数组中
 * Data类位于com.atguigu.team.service包中
 * @date 2025/2/15 21:48
 */

public class NameListService {
    private Employee[] employees;

    public NameListService() {
        employees= new Employee[EMPLOYEES.length];
            for(int i=0;i<employees.length;i++){
                int type=Integer.parseInt(EMPLOYEES[i][0]);//区分程序员还是工程师.
                int id=Integer.parseInt(EMPLOYEES[i][1]);//id
                 String name=EMPLOYEES[i][2];//名字
                int age=Integer.parseInt(EMPLOYEES[i][3]);//年龄
                double salary=Integer.parseInt(EMPLOYEES[i][4]);//薪水

                Equipment equipment;
                double bonus;
                int stock;
                switch (type){
                    case EMPLOYEE :
                        employees[i]=new Employee(id,name,age,salary);
                        break;
                    case PROGRAMMER:
                        equipment=createEquipment(i);
                        employees[i]=new Programmer(id,name,age,salary,equipment);
                        break;
                    case DESIGNER:
                        equipment=createEquipment(i);
                        bonus=Double.parseDouble(EMPLOYEES[i][5]);
                        employees[i]=new Designer(id,name,age,salary,equipment,bonus);
                        break;
                    case ARCHITECT:
                        equipment=createEquipment(i);
                        bonus = Integer.parseInt(EMPLOYEES[i][5]);
                        stock = Integer.parseInt(EMPLOYEES[i][6]);
                        employees[i]=new Architect(id,name,age,salary,equipment,bonus,stock);
                        break;
                    }
            }
    }
    public Equipment createEquipment(int index){
        int type = Integer.parseInt(EQIPMENTS[index][0]);
        switch (type){
            case PC :
                    return new PC(EQIPMENTS[index][1],EQIPMENTS[index][2]);
            case NOTEBOOK:
                int price=Integer.parseInt(EQIPMENTS[index][2]);
                return new NoteBook(EQIPMENTS[index][1],price);
            case PRINTER:
                return new Printer(EQIPMENTS[index][1],EQIPMENTS[index][2]);
            }
            return null;
    }


    public Employee[] getAllEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException{
        for(int i=0;i<employees.length;i++){
            if(employees[i].getId()==id){
                return employees[i];
            }
        }
        throw new TeamException("该员工不存在");
    }
}